package com.example.jvm.jvm05.lesson31;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.openjdk.jol.datamodel.X86_32_DataModel;
import org.openjdk.jol.datamodel.X86_64_COOPS_DataModel;
import org.openjdk.jol.datamodel.X86_64_DataModel;
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.layouters.HotSpotLayouter;
import org.openjdk.jol.layouters.Layouter;
import org.openjdk.jol.vm.VM;

/**
 * @Description
 * @Date 2021/7/13
 * @Created by Jonathan
 */
@Slf4j
public class ObjectTest {
    @Test
    public void test1() {
        Layouter l;
        l = new HotSpotLayouter(new X86_32_DataModel());
        log.info(ClassLayout.parseInstance(new MyObject("111", new Student(), 111, 1, 1L), l).toPrintable());
        log.info("===========分割线=================");
        l = new HotSpotLayouter(new X86_64_COOPS_DataModel());
        log.info(ClassLayout.parseInstance(new MyObject("222", new Student(), 222, 2, 2L), l).toPrintable());
    }

    @Test
    public void test2() {
        int[] a = {1};
        Layouter l;
        l = new HotSpotLayouter(new X86_32_DataModel());
        log.info(ClassLayout.parseInstance(a, l).toPrintable());
        log.info("===========分割线=================");
        l = new HotSpotLayouter(new X86_64_COOPS_DataModel());
        log.info(ClassLayout.parseInstance(a, l).toPrintable());
    }

    @Test
    public void test3() {
        int[] a = {1};
        Layouter l;
        l = new HotSpotLayouter(new X86_32_DataModel());
        MyObject o = new MyObject();
        log.info("十六进制：{}", Integer.toHexString(o.hashCode()));
        log.info(ClassLayout.parseInstance(o, l).toPrintable());
    }

    @Test
    public void testIntegerLayout() {
        Integer a = new Integer(10);
        Layouter l;
        l = new HotSpotLayouter(new X86_64_DataModel());
//        MyObject o = new MyObject();
        System.out.println(VM.current().details());
//        log.info("十六进制：{}", Integer.toHexString(o.hashCode()));
        new Thread(() -> {
            synchronized (a) {
                log.info(ClassLayout.parseInstance(a, l).toPrintable());
                log.info("hashCode:{}", a.hashCode());
                log.info(ClassLayout.parseInstance(a, l).toPrintable());
            }
        }).start();
    }
}
